/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tolls;

import avenue.MyWindowEvent;
import avenue.Session;
import avenue.Utils;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import model.DBManager;
import model.ProgramRate;
import model.Vehicle;

/**
 *
 * @author Aggelos
 */
public class CollectTollForm extends javax.swing.JFrame {

    int module_Id = Session.TOLL_COLLECTION_MODULE;
    private Vehicle vehicle;
    private JFrame thisFrame;
    double rate;

    public CollectTollForm() {
        em = DBManager.em;
        if (!em.getTransaction().isActive()) {
            em.getTransaction().begin();
        }
        initComponents();
        jTxtBalance.setHorizontalAlignment(JTextField.RIGHT);
        jΤxtRate.setHorizontalAlignment(JTextField.RIGHT);

        jΤxtRate.setHorizontalAlignment(JTextField.RIGHT);
        jΤxtLastMonthPasses.setHorizontalAlignment(JTextField.RIGHT);
        jΤxtLastMonthFreePasses.setHorizontalAlignment(JTextField.RIGHT);
        checkControls();
        Utils.centerOnScreen(this);
        this.getRootPane().setDefaultButton(jBtnCheck);
        //jPnInfos.setVisible(false);
    }

    private void checkControls() {
        if (jTxtRegNumber.getText().trim().equals("") || jTxtCard.getText().trim().equals("")) {
            jBtnCheck.setEnabled(false);
        } else {
            jBtnCheck.setEnabled(true);
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLbMessage = new javax.swing.JLabel();
        jPnInfos = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jΤxtStation = new javax.swing.JTextField();
        jΤxtRate = new javax.swing.JTextField();
        jΤxtLastMonthFreePasses = new javax.swing.JTextField();
        jΤxtLastMonthPasses = new javax.swing.JTextField();
        jΤxtProgram = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTxtBalance = new javax.swing.JTextField();
        jBtnPay = new javax.swing.JButton();
        jBtnPass = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jTxtCatecory = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jBtnCheck = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTxtRegNumber = new javax.swing.JTextField();
        jTxtCard = new javax.swing.JTextField();
        jBtnCancel2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Διέλευση Οχήματος");
        setResizable(false);

        jLbMessage.setBackground(new java.awt.Color(255, 255, 255));
        jLbMessage.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLbMessage.setText(" ");

        jPnInfos.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel4.setForeground(new java.awt.Color(0, 51, 102));
        jLabel4.setText(".............");

        jLabel6.setText("Χρέωση:");

        jLabel5.setText("Σταθμός:");

        jLabel7.setForeground(new java.awt.Color(0, 51, 102));
        jLabel7.setText("Δωρεάν:");

        jΤxtStation.setEditable(false);

        jΤxtRate.setEditable(false);
        jΤxtRate.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jΤxtRate.setForeground(new java.awt.Color(255, 0, 51));

        jΤxtLastMonthFreePasses.setEditable(false);
        jΤxtLastMonthFreePasses.setForeground(new java.awt.Color(0, 0, 102));

        jΤxtLastMonthPasses.setEditable(false);
        jΤxtLastMonthPasses.setForeground(new java.awt.Color(0, 51, 102));

        jΤxtProgram.setEditable(false);

        jLabel8.setText("Πρόγραμμα:");

        jLabel3.setText("Υπόλοιπο Κάρτας: ");

        jTxtBalance.setEditable(false);
        jTxtBalance.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N

        jBtnPay.setText("ΠΛΗΡΩΜΗ");
        jBtnPay.setEnabled(false);
        jBtnPay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnPayActionPerformed(evt);
            }
        });

        jBtnPass.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        jBtnPass.setText("Διέλευση");
        jBtnPass.setEnabled(false);
        jBtnPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnPassActionPerformed(evt);
            }
        });

        jLabel9.setText("Κατηγορία:");

        jTxtCatecory.setEditable(false);
        jTxtCatecory.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel10.setForeground(new java.awt.Color(0, 51, 102));
        jLabel10.setText("Σύνολο:");

        jLabel11.setForeground(new java.awt.Color(0, 0, 102));
        jLabel11.setText("Διελεύσεις από");

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 44, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPnInfosLayout = new javax.swing.GroupLayout(jPnInfos);
        jPnInfos.setLayout(jPnInfosLayout);
        jPnInfosLayout.setHorizontalGroup(
            jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnInfosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnInfosLayout.createSequentialGroup()
                        .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPnInfosLayout.createSequentialGroup()
                                .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 1, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel10))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jΤxtLastMonthFreePasses, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jΤxtLastMonthPasses, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPnInfosLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jΤxtRate, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jBtnPay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBtnPass, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)))
                    .addGroup(jPnInfosLayout.createSequentialGroup()
                        .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPnInfosLayout.createSequentialGroup()
                                .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jΤxtStation)
                                    .addComponent(jΤxtProgram, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
                                    .addComponent(jTxtCatecory)))
                            .addGroup(jPnInfosLayout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jTxtBalance, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(200, 200, 200)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPnInfosLayout.setVerticalGroup(
            jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnInfosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jTxtCatecory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jΤxtStation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jΤxtProgram, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                        .addComponent(jTxtBalance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addComponent(jBtnPay, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPnInfosLayout.createSequentialGroup()
                        .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jΤxtLastMonthPasses, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jΤxtLastMonthFreePasses, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jΤxtRate)
                            .addComponent(jLabel6)))
                    .addComponent(jBtnPass, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPnInfosLayout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addGroup(jPnInfosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPnInfosLayout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addGap(6, 6, 6))
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(46, 46, 46)))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jBtnCheck.setText("Έλεγχος");
        jBtnCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCheckActionPerformed(evt);
            }
        });

        jLabel1.setText("Αρ. Κυκλοφορίας: ");

        jLabel2.setText("Αρ. Κάρτας: ");

        jTxtRegNumber.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTxtRegNumberKeyReleased(evt);
            }
        });

        jTxtCard.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTxtCardKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(11, 11, 11))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTxtRegNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTxtCard, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBtnCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTxtRegNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTxtCard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)))
                    .addComponent(jLabel1))
                .addContainerGap())
        );

        jBtnCancel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        jBtnCancel2.setText("Ακυρο");
        jBtnCancel2.setFocusable(false);
        jBtnCancel2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancel2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLbMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(200, 200, 200))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPnInfos, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 9, Short.MAX_VALUE))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jBtnCancel2, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnInfos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnCancel2)
                .addGap(1, 1, 1)
                .addComponent(jLbMessage)
                .addGap(0, 0, 0))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnCancel2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancel2ActionPerformed
        // TODO add your handling code here:
        em.getTransaction().rollback();
        em.getTransaction().begin();
        dispose();
    }//GEN-LAST:event_jBtnCancel2ActionPerformed

    private void jBtnPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnPassActionPerformed
        if (!Session.userHasRolePermission(module_Id, Session.CAN_CREATE)) {
            return;
        }
        model.Collection c = new model.Collection();
        vehicle.getCardId().setBalance(vehicle.getCardId().getBalance() - rate);
        c.setStationId(Session.loginUser.getStationId());
        c.setCardId(vehicle.getCardId());
        c.setTollCollected(BigDecimal.valueOf(rate));
        c.setPassTimestamp(new Date());
        em.persist(c);
        em.getTransaction().commit();
        em.getTransaction().begin();
        dispose();
    }//GEN-LAST:event_jBtnPassActionPerformed

    private void jBtnCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCheckActionPerformed

        vehicle = Utils.VehicleByRegNumber(jTxtRegNumber.getText());
        //jLbRate.setText("");
        jTxtBalance.setText("0.00");

        if (vehicle != null) {
            if (vehicle.getCardId().getActive() == 0) {
                jBtnPass.setEnabled(false);
                jBtnPay.setEnabled(false);
                JOptionPane.showMessageDialog(this, Utils.msgCardIsInactive, Utils.msgDialogTitle, JOptionPane.WARNING_MESSAGE);
                return;
            }
            if (vehicle.getCardId().getBarcode() == Integer.parseInt(jTxtCard.getText())) {
                jTxtBalance.setText(String.format("%.2f", vehicle.getCardId().getBalance()));
                jΤxtStation.setText(Session.loginUser.getStationId().getDescription());
                jTxtCatecory.setText(vehicle.getCategoryId().getDescription());
                jΤxtProgram.setText(vehicle.getCardId().getProgramId().getDescription());
                jPnInfos.setVisible(true);


                SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");

                jLabel4.setText(f.format(Utils.beginOfThisMonth(new Date())));



                List<Collection> c = DBManager.em.createQuery("SELECT c FROM Collection c where c.cardId=:cardId and c.passTimestamp>=:d").setParameter("cardId", vehicle.getCardId()).setParameter("d", Utils.beginOfThisMonth(new Date())).getResultList();

                int monthPasses = c.size();

                c = DBManager.em.createQuery("SELECT c FROM Collection c where c.cardId=:cardId and c.tollCollected=0 and c.passTimestamp>=:d").setParameter("cardId", vehicle.getCardId()).setParameter("d", Utils.beginOfThisMonth(new Date())).getResultList();

                int monthFreePassNumber = c.size();

                jΤxtLastMonthPasses.setText(String.format("%d", monthPasses));
                jΤxtLastMonthFreePasses.setText(String.format("%d", monthFreePassNumber));

                if ((monthPasses >= vehicle.getCardId().getProgramId().getFreePassThreshold())
                        && ((monthFreePassNumber < vehicle.getCardId().getProgramId().getFreePassNumber()))) {
                    rate = 0f;
                    jΤxtRate.setText("ΔΩΡΕΑΝ");
                    jBtnPass.setEnabled(true);
                } else {

                    rate = 9999999999.0;

                    for (ProgramRate pr : vehicle.getCardId().getProgramId().getProgramRateList()) {
                        if ((pr.getCategoryId() == vehicle.getCategoryId()) && (pr.getStationId() == Session.loginUser.getStationId())) {
                            rate = (float) pr.getRate().doubleValue();
                            jΤxtRate.setText(String.format("%.2f", pr.getRate()));
                            break;
                        }
                    }

                    jBtnPass.setEnabled(rate <= vehicle.getCardId().getBalance());
                    jBtnPay.setEnabled(true);
                    if (vehicle.getCardId().getBalance() < 5) {
                        jLbMessage.setText(Utils.msgLowBalance);
                    }
                    if (jBtnPass.isEnabled()) {
                        this.getRootPane().setDefaultButton(jBtnPass);
                    } else {
                        this.getRootPane().setDefaultButton(jBtnPay);
                    }
                }

            } else {
                jBtnPass.setEnabled(false);
                JOptionPane.showMessageDialog(this, Utils.msgPassDenied, Utils.msgDialogTitle, JOptionPane.WARNING_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, Utils.msgPassDenied, Utils.msgDialogTitle, JOptionPane.WARNING_MESSAGE);

        }

    }//GEN-LAST:event_jBtnCheckActionPerformed

    private void jBtnPayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnPayActionPerformed
        if (!Session.userHasRolePermission(Session.PAYMENTS_MODULE, Session.CAN_CREATE)) {
            return;
        }
        PaymentForm pf = new PaymentForm(vehicle);

        pf.setVisible(true);
        thisFrame = this;
        thisFrame.setEnabled(false);

        pf.addWindowListener(new WindowListener() {
            public void windowClosed(WindowEvent arg0) {
                System.out.println("Window close event occur");
                if (MyWindowEvent.isExitAndSave(arg0)) {


                    thisFrame.setEnabled(true);
                    dispose();
                } else {
                    thisFrame.setEnabled(true);
                    dispose();
                }
                checkControls();
            }

            public void windowActivated(WindowEvent arg0) {
                System.out.println("Window Activated");
            }

            public void windowClosing(WindowEvent arg0) {
                System.out.println("Window Closing");
            }

            public void windowDeactivated(WindowEvent arg0) {
                System.out.println("Window Deactivated");
            }

            public void windowDeiconified(WindowEvent arg0) {
                System.out.println("Window Deiconified");
            }

            public void windowIconified(WindowEvent arg0) {
                System.out.println("Window Iconified");
            }

            public void windowOpened(WindowEvent arg0) {
                System.out.println("Window Opened");
            }
        });
        //************     
    }//GEN-LAST:event_jBtnPayActionPerformed

    private void jTxtRegNumberKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtRegNumberKeyReleased
        checkControls();
    }//GEN-LAST:event_jTxtRegNumberKeyReleased

    private void jTxtCardKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtCardKeyReleased
        checkControls();        // TODO add your handling code here:
    }//GEN-LAST:event_jTxtCardKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CollectTollForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CollectTollForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CollectTollForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CollectTollForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CollectTollForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCancel2;
    private javax.swing.JButton jBtnCheck;
    private javax.swing.JButton jBtnPass;
    private javax.swing.JButton jBtnPay;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLbMessage;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPnInfos;
    private javax.swing.JTextField jTxtBalance;
    private javax.swing.JTextField jTxtCard;
    private javax.swing.JTextField jTxtCatecory;
    private javax.swing.JTextField jTxtRegNumber;
    private javax.swing.JTextField jΤxtLastMonthFreePasses;
    private javax.swing.JTextField jΤxtLastMonthPasses;
    private javax.swing.JTextField jΤxtProgram;
    private javax.swing.JTextField jΤxtRate;
    private javax.swing.JTextField jΤxtStation;
    // End of variables declaration//GEN-END:variables
    EntityManager em;
}
